﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Order.DAInterface;
using CYTS.Aoyou.Framework.Database;
using CYTS.Aoyou.Order.Entity;
using System.Data;
using System.Data.SqlClient;
using CYTS.Aoyou.Framework.Log;

namespace CYTS.Aoyou.Order.DataAccess
{
    public class OrderInfoDataAdapterForSqlServer : DataAdapterBase, IOrderInfoDataAdapter, Framework.Database.IDataAdapter
    {
        public OrderInfoDataAdapterForSqlServer(DBProvider provider)
            : base(provider)
        { }

        public OrderInfo GetOrderInfo(int orderID)
        {
            OrderInfo orderInfo = null;
            string dbConn = string.Empty;
            if (!this.DicDBConnection.TryGetValue("CYTSERP_Read", out dbConn))
            {
                throw new Exception("未找到对应的数据库连接串");
            }

            SqlParameter param = new SqlParameter("@OrderID", SqlDbType.Int);
            param.Value = orderID;
            DataSet ds = null;
            try
            {
                ds = SQLHelper.ExecuteDataset(dbConn,
                    System.Data.CommandType.Text,
                    "SELECT [Group_Name],[Order_Money],[Total_Actual] FROM [Sub_Order] WHERE [Sub_Order_ID] = @OrderID ",
                    new SqlParameter[] { param });
            }
            catch (Exception ex)
            {
                throw new Exception("数据库查询报错", ex);
            }

            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                var dr = ds.Tables[0].Rows[0];
                orderInfo = new OrderInfo();
                orderInfo.OrderID = orderID;
                orderInfo.ProductName = dr.Field<string>("Group_Name");
                orderInfo.OrderMoney = dr.Field<decimal>("Order_Money");
                orderInfo.PaidMoney = dr.Field<decimal>("Total_Actual");
            }

            return orderInfo;
        }
    }
}
